1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| #pragma GCC optimize(2) #include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int MAXN=2e5+5; const int MAXM=2e5+5; struct node { int op,x,y1,y2,val,ID; } qu[MAXN+MAXM*4],qu1[MAXN+MAXM*4]; int a[MAXN],b[MAXN],pa[MAXN],pb[MAXN]; int c[MAXN]; int ans[MAXM]; int n,m,nn,nnn; inline int read() { int sum=0;char ch=getchar(); while (ch>'9'||ch<'0') ch=getchar(); while (ch>='0'&&ch<='9') sum=sum*10+ch-'0',ch=getchar(); return sum; } inline void update(int x,int val) { for (;x<=n;x+=x&-x) c[x]+=val; } inline int query(int x) { int sum=0; for (;x;x-=x&-x) sum+=c[x]; return sum; } inline void clear(int x) { for (;x<=n;x+=x&-x) { if (!c[x]) break; c[x]=0; } } void cdq(int l,int r) { if (l==r) return; int mid=(l+r)>>1; cdq(l,mid);cdq(mid+1,r); int s1=l,s2=mid+1,s3=l-1; while (s1<=mid && s2<=r) { if (qu[s1].x<=qu[s2].x) { if (qu[s1].op==2) update(qu[s1].y1,qu[s1].val); qu1[++s3]=qu[s1];s1++; } else { if (qu[s2].op==1) ans[qu[s2].ID]+=qu[s2].val*(query(qu[s2].y2)-query(qu[s2].y1-1)); qu1[++s3]=qu[s2];s2++; } } while (s1<=mid) qu1[++s3]=qu[s1],s1++; while (s2<=r) { if (qu[s2].op==1) ans[qu[s2].ID]+=qu[s2].val*(query(qu[s2].y2)-query(qu[s2].y1-1)); qu1[++s3]=qu[s2];s2++; } for (int i=l;i<=r;i++) { if (qu1[i].op==2) clear(qu1[i].y1); qu[i]=qu1[i]; } } int main() { n=read(),m=read(); for (int i=1;i<=n;i++) a[i]=read(),pa[a[i]]=i; for (int i=1;i<=n;i++) b[i]=read(),pb[b[i]]=i; for (int i=1;i<=n;i++) qu[++nn]=(node){2,i,pb[a[i]],pb[a[i]],1,0}; for (int i=1;i<=m;i++) { int op=read(); if (op==1) { nnn++; int la=read(),ra=read(),lb=read(),rb=read(); qu[++nn]=(node){1,la-1,lb,rb,-1,nnn}; qu[++nn]=(node){1,ra,lb,rb,1,nnn}; } else { int x=read(),y=read(); qu[++nn]=(node){2,pa[b[x]],x,x,-1,0}; qu[++nn]=(node){2,pa[b[x]],y,y,1,0}; qu[++nn]=(node){2,pa[b[y]],y,y,-1,0}; qu[++nn]=(node){2,pa[b[y]],x,x,1,0}; swap(b[x],b[y]); } } cdq(1,nn); for (int i=1;i<=nnn;i++) printf("%d\n",ans[i]); return 0; }
|